const path = require("path");
const webpack = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const htmlwebpackplugin = require("html-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
//process.env.NODE_ENV = "development";
module.exports = {
  entry: path.resolve(__dirname, "./src/js/index.js"),
  output: {
    filename: "js/[name].js",
    path: path.resolve(__dirname, "./dist"),
    publicPath: "/", // /代表url跟路径  打包时改为‘./’
  },
  devtool: "cheap-module-eval-source-map",
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [
              [
                "@babel/preset-env",
                {
                  //按需加载
                  useBuiltIns: "usage",
                  //知道版本
                  corejs: {
                    version: 3,
                    proposals: true,
                  },
                  //指定兼容的浏览器
                  targets: {
                    ie: 10,
                    chrome: "60",
                    firefox: "60",
                    safari: "10",
                  },
                },
              ],
            ],
          },
        },
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              //publicPath: "../../dist/",//打包时添加
            },
          },
          "css-loader",
          {
            loader: "postcss-loader",
          },
        ],
      },
      {
        test: /\.less$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          {
            loader: "postcss-loader",
          },
          "less-loader",
        ],
      },
      {
        test: /\.(png|jpg|gif|jpeg|webp|svg|eot|ttf|woff|woff2)$/,
        exclude: /node_modules/,
        use: {
          loader: "url-loader",
          options: {
            //10k
            limit: 10240,
            //生成资源名称
            name: "[name].[hash:8].[ext]",
            //生成资源的路径
            outputPath: "imgs/",
            //默认是用es6模块化解析，而html是用commom.js解析方式
            esModule: false,
          },
        },
      },
      {
        test: /\.html$/,
        //处理html的img,采用的是commom.js解析方式
        loader: "html-loader",
      },
    ],
  },
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    compress: true,
    port: 9000,
    host: "0.0.0.0",
    //自动打开浏览器
    open: false,
    //启用模块热替换
    //hot: true,
  },
  plugins: [
    new htmlwebpackplugin({
      template: "./src/index.html",
    }),
    new MiniCssExtractPlugin({
      filename: "css/[name].css",
    }),
    new CleanWebpackPlugin(),
    //new webpack.HotModuleReplacementPlugin({}),
    new OptimizeCSSAssetsPlugin(),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, "public"),
          to: path.resolve(__dirname, "dist", "static"),
          flatten: true,
        },
      ],
    }),
  ],
  mode: "development",
};
